package com.caioweb.osa.conversores;

import java.io.Serializable;
import java.util.List;

import javax.faces.bean.RequestScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.inject.Inject;

import com.caioweb.osa.dao.BranchDAO;
import com.caioweb.osa.modelo.Branch;


@FacesConverter("conversorbranch")
@RequestScoped
public class ConversorBranch implements Converter,Serializable{
	@Inject
	private BranchDAO dao;
	

	@Override
	public Object getAsObject(FacesContext context, UIComponent ui, String nome) {
		List<Branch> lista= dao.existeBranch(nome);
		if(!lista.isEmpty()){
			return lista.get(0);
		}
		return null;
	}

	@Override
	public String getAsString(FacesContext context, UIComponent ui, Object o) {
		Branch branch = new Branch();
		if(o instanceof Branch){
		branch = (Branch)  o;
		return branch.getNome();
		}
		return "";
	}



}
